<template>
  <div id="userRegisterPage">
    <div class="register-container">
      <div class="register-header">
        <h1 class="title">木棉 AI 代码生成器</h1>
        <div class="desc">不写一行代码，生成完整应用</div>
      </div>

      <div class="register-form-wrapper">
        <h2 class="form-title">用户注册</h2>
        <a-form
          :model="formState"
          name="register"
          autocomplete="off"
          @finish="handleSubmit"
          layout="vertical"
        >
          <a-form-item
            name="userAccount"
            :rules="[
              { required: true, message: '请输入账号' },
              { min: 4, message: '账号不能少于4位' },
              { pattern: /^[a-zA-Z0-9_]+$/, message: '账号只能包含字母、数字和下划线' },
            ]"
          >
            <a-input
              v-model:value="formState.userAccount"
              placeholder="请输入账号"
              size="large"
              :prefix="h(UserOutlined)"
            />
          </a-form-item>

          <a-form-item
            name="userPassword"
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 8, message: '密码不能少于8位' },
              { pattern: /^(?=.*[a-zA-Z])(?=.*\d).+$/, message: '密码必须包含字母和数字' },
            ]"
          >
            <a-input-password
              v-model:value="formState.userPassword"
              placeholder="请输入密码"
              size="large"
              :prefix="h(LockOutlined)"
            />
          </a-form-item>

          <a-form-item
            name="checkPassword"
            :rules="[
              { required: true, message: '请确认密码' },
              { validator: validateConfirmPassword },
            ]"
          >
            <a-input-password
              v-model:value="formState.checkPassword"
              placeholder="请确认密码"
              size="large"
              :prefix="h(LockOutlined)"
            />
          </a-form-item>

          <a-form-item>
            <a-button
              type="primary"
              html-type="submit"
              size="large"
              class="register-button"
              :loading="loading"
            >
              立即注册
            </a-button>
          </a-form-item>

          <div class="login-link">
            <span>已有账号？</span>
            <router-link to="/user/login" class="link">立即登录</router-link>
          </div>
        </a-form>
      </div>

      <div class="register-footer">
        <div class="social-register">
          <div class="divider">
            <span>或使用以下方式注册</span>
          </div>
          <div class="social-buttons">
            <a-button class="social-btn github" :icon="h(GithubOutlined)"></a-button>
            <a-button class="social-btn wechat" :icon="h(WechatOutlined)"></a-button>
            <a-button class="social-btn qq" :icon="h(QqOutlined)"></a-button>
          </div>
        </div>

        <div class="copyright">© 2024 木棉 AI 代码生成器. All rights reserved.</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, h } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { userRegister } from '@/api/userController'
import {
  UserOutlined,
  LockOutlined,
  WechatOutlined,
  QqOutlined,
  GithubOutlined,
} from '@ant-design/icons-vue'

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

const router = useRouter()
const loading = ref(false)

/**
 * 验证确认密码
 */
const validateConfirmPassword = async (_rule: any, value: string) => {
  if (value && value !== formState.userPassword) {
    return Promise.reject('两次输入的密码不一致')
  }
  return Promise.resolve()
}

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  loading.value = true
  try {
    const res = await userRegister(values)
    if (res.data.code === 0) {
      message.success('注册成功，即将跳转到登录页面')
      // 注册成功后跳转到登录页面
      setTimeout(() => {
        router.push({
          path: '/user/login',
          replace: true,
        })
      }, 1500)
    } else {
      message.error('注册失败，' + res.data.message)
    }
  } catch (error) {
    message.error('注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
#userRegisterPage {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 75vh;
  background: linear-gradient(
    135deg,
    #4ade80 0%,
    #22c55e 25%,
    #16a34a 50%,
    #15803d 75%,
    #166534 100%
  );
  padding: 20px;
  position: relative;
}

.register-container {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px 30px;
  position: relative;
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
  backdrop-filter: blur(10px);
}

.register-container:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

.register-header {
  text-align: center;
  margin-bottom: 36px;
}

.title {
  font-size: 28px;
  font-weight: 700;
  color: #16a34a;
  margin-bottom: 12px;
  letter-spacing: 0.5px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.desc {
  font-size: 14px;
  color: #666;
  font-weight: 400;
  line-height: 1.4;
}

.register-form-wrapper {
  margin-bottom: 32px;
}

.form-title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin-bottom: 24px;
  text-align: center;
}

.ant-form-item {
  margin-bottom: 20px;
}

.ant-input,
.ant-input-password {
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s ease;
}

.ant-input:focus,
.ant-input-password:focus,
.ant-input-focused,
.ant-input-password-focused {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
}

.register-button {
  width: 100%;
  height: 44px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border: none;
  transition: all 0.3s ease;
}

.register-button:hover {
  background: linear-gradient(135deg, #096dd9 0%, #0050b3 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.login-link {
  text-align: center;
  margin-top: 16px;
  font-size: 14px;
  color: #666;
}

.login-link .link {
  color: #1890ff;
  text-decoration: none;
  font-weight: 500;
  margin-left: 4px;
  transition: color 0.3s ease;
}

.login-link .link:hover {
  color: #096dd9;
  text-decoration: underline;
}

.register-footer {
  border-top: 1px solid #f0f0f0;
  padding-top: 24px;
}

.social-register {
  margin-bottom: 20px;
}

.divider {
  text-align: center;
  margin-bottom: 16px;
  position: relative;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e0e0e0;
}

.divider span {
  background: rgba(255, 255, 255, 0.95);
  padding: 0 16px;
  font-size: 12px;
  color: #999;
  position: relative;
  z-index: 1;
}

.social-buttons {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.social-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid #e0e0e0;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.social-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.social-btn.github:hover {
  background: #333;
  color: #fff;
  border-color: #333;
}

.social-btn.wechat:hover {
  background: #07c160;
  color: #fff;
  border-color: #07c160;
}

.social-btn.qq:hover {
  background: #12b7f5;
  color: #fff;
  border-color: #12b7f5;
}

.copyright {
  text-align: center;
  font-size: 12px;
  color: #999;
  line-height: 1.4;
}

/* 响应式设计 */
@media (max-width: 480px) {
  #userRegisterPage {
    padding: 10px;
    min-height: 70vh;
  }

  .register-container {
    padding: 30px 20px;
    max-width: 100%;
  }

  .title {
    font-size: 24px;
  }

  .form-title {
    font-size: 18px;
  }

  .social-buttons {
    gap: 8px;
  }

  .social-btn {
    width: 40px;
    height: 40px;
  }
}
</style>
